package com.AFH.test;

import java.util.HashMap;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class WelcomeView extends SurfaceView implements SurfaceHolder.Callback {

	ViewSwitcher vs;
	WelcomeViewDrawThread wvdt;
	int selectedIndex = -1;
	int isTouch;
	int alpha = 0;
	int framemove = 0;
	// Rect Play = new Rect(390,320,452,388);
	Rect Play = new Rect(392, 260, 479, 424);
	Rect Quit = new Rect(285, 210, 372, 374);
	Rect About = new Rect(500, 210, 587, 374);
	Rect sound = new Rect(720,0,800,80);
	SoundPool WelcomeMicPool= new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
	
	HashMap<Integer, Integer> soundMap;

	public WelcomeView(ViewSwitcher vs) {
		super(vs);
		this.vs = vs;
		getHolder().addCallback(this);
		initSound(vs);
		wvdt = new WelcomeViewDrawThread(this, getHolder());
		isTouch = -1;
		// initSound(vs);
	}

	public void doDraw(Canvas canvas) {

		Paint paint = new Paint();
		paint.setAlpha(alpha);
		BitmapManager.DrawWelcomeResource(0, canvas, 0, 0, null);
		BitmapManager.DrawWelcomeResource(1, canvas, -227 + framemove * 9.8f,
				45, null);
		BitmapManager.DrawWelcomeResource(2, canvas, 800 - framemove * 9.125f,
				45, null);
		BitmapManager.DrawWelcomeResource(4, canvas, -124 + framemove * 9.05f,
				115, null);
		BitmapManager.DrawWelcomeResource(3, canvas, 800 - framemove * 10.2f,
				115, null);
		BitmapManager.DrawWelcomeResource(11, canvas, 500, 210, paint);
		BitmapManager.DrawWelcomeResource(8, canvas, 285, 210, paint);
		BitmapManager.DrawWelcomeResource(5, canvas, 392, 260, paint);
		BitmapManager.DrawWelcomeResource(6, canvas, 720, 0, paint);
		switch (isTouch) {
		case 1:
			BitmapManager.DrawWelcomeResource(9, canvas, 392, 260, null);
			new Thread() {
				@Override
				public void run() {
					try {
						sleep(1500);

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				

				}
				
			}.start();
			vs.AfhHandler.sendEmptyMessage(0);
			// isTouch = -1;
			break;
		case 3:
			BitmapManager.DrawWelcomeResource(10, canvas, 285, 210, null);
			new Thread() {
				@Override
				public void run() {
					try {
						sleep(1500);

					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}.start();
			vs.AfhHandler.sendEmptyMessage(2);
			// isTouch = -1;
			break;
		case 4:
			vs.AfhHandler.sendEmptyMessage(3);
			
			break;
		case 5:
			if(vs.wantSound){
				BitmapManager.DrawWelcomeResource(6, canvas, 720, 0, null);
			}
			else{
				BitmapManager.DrawWelcomeResource(7, canvas, 720, 0, null);
			}
			break;
		default:
			break;
		}

	}

	public boolean touchEvent(int x, int y) {
		if (Play.contains(x, y)) {
			selectedIndex = 0;
			isTouch = 1;
		}
		if (Quit.contains(x, y)) {
			selectedIndex = 2;
			isTouch = 3;
		}
		if (About.contains(x, y)) {
			selectedIndex = 3;
			isTouch = 4;
		}
		if(sound.contains(x, y)){
			selectedIndex = 4;
			if(vs.wantSound){	
				stopsound(1);
				vs.volume = 0;
				vs.wantSound=false;
				
				
			}
		
			else if(!vs.wantSound){
				initSound(vs);
				WelcomeMicPool.resume(1);
				playSound(1, -1);
				AudioManager am=(AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
				float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);   
			    float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);       
				vs.volume= streamVolumeCurrent/streamVolumeMax;
				
				vs.wantSound=true;
			}
			
			isTouch = 5;
		}
		return true;

	}

	public void initSound(ViewSwitcher vs) {
		
		soundMap = new HashMap<Integer, Integer>();
		// int id = WelcomeMicPool.load(vs,R.raw.welcom_background, 1);
		soundMap.put(1, WelcomeMicPool.load(vs, R.raw.welcomebgm1, 1));

	}

	public void playSound(int MicNum, int loop) {
		AudioManager am = (AudioManager) getContext().getSystemService(
				Context.AUDIO_SERVICE);
		float streamVolumeCurrent = am
				.getStreamVolume(AudioManager.STREAM_MUSIC);
		float streamVolumeMax = am
				.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		float volume = streamVolumeCurrent / streamVolumeMax;
		WelcomeMicPool.play(soundMap.get(MicNum), volume, volume, 1, loop, 1f);
	}

	public void stopsound(int MicNum) {
		WelcomeMicPool.pause(MicNum);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		wvdt.DrawOn = true;
		if (!wvdt.isAlive()) {
			wvdt.start();
		}
		if (vs.wantSound) {
			playSound(1, -1);
			// WelcomeMicPool.play(-1, 1f, 1f, 1, -1, 1f);
		}

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		wvdt.DrawOn = false;
		wvdt.flag=false;
	}

}